package com.shaoshaohuo.app.ui.shipper;

import java.util.ArrayList;
import java.util.List;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.constant.ListViewAction;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.Order;
import com.shaoshaohuo.app.entity.OrderListEntity;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.adapter.OrderListAdapter;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;
import com.shaoshaohuo.app.view.XListView.IXListViewListener;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class MyOrderTailAfterActivity extends BaseActivity implements OnClickListener, IXListViewListener {

//	private static final int STATUS_ALL = 0;
//	private static final int STATUS_WILL_DETERMINED = 1;
//	private static final int STATUS_HANDING = 2;
//	private static final int STATUS_FINISH = 3;
	private static final int STATUS_YUNSHUZHONG = 4;
	
	private TopbarView mTopbarView;
	
//	private TextView mDaiquedingText;
//	private TextView mJinxingzhongText;
//	private TextView mYiwanchengText;

	private XListView mOrderListview;
	private OrderListAdapter<Order> mAdapter;
	
	private List<Order> mDataList = new ArrayList<Order>();
	private int size = 10;
	private String action = ListViewAction.NEW;
	private String cursor = "0";
	private int searchstatus = STATUS_YUNSHUZHONG;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_order_tail_after);
		initView();
		setUpView();
		
		getOrderList(true);
	}

	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);

//		mDaiquedingText = (TextView) this.findViewById(R.id.textview_daiqueding);
//		mJinxingzhongText = (TextView) this.findViewById(R.id.textview_jinxingzhong);
//		mYiwanchengText = (TextView) this.findViewById(R.id.textview_yiwancheng);
		
		mOrderListview = (XListView) this.findViewById(R.id.listview_cargo_list);
	}

	private void setUpView() {
		mTopbarView.setCenterText("我的车队");
		mTopbarView.setLeftView(true, true);
		
//		mDaiquedingText.setOnClickListener( this);
//		mJinxingzhongText.setOnClickListener( this);
//		mYiwanchengText.setOnClickListener( this);
//	
////		onClick(mDaiquedingText);
//		mDaiquedingText.setSelected(true);
//		mJinxingzhongText.setSelected(false);
//		mYiwanchengText.setSelected(false);
		
		setListView();
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
//		case R.id.textview_daiqueding:
//			if(mDaiquedingText.isSelected()){
//				return;
//			}
//			mDaiquedingText.setSelected(true);
//			mJinxingzhongText.setSelected(false);
//			mYiwanchengText.setSelected(false);
//			
//			changeStatus(STATUS_WILL_DETERMINED);
//			break;
//		case R.id.textview_jinxingzhong:
//			if(mJinxingzhongText.isSelected()){
//				return;
//			}
//			mDaiquedingText.setSelected(false);
//			mJinxingzhongText.setSelected(true);
//			mYiwanchengText.setSelected(false);
//			
//			changeStatus(STATUS_HANDING);
//			break;
//		case R.id.textview_yiwancheng:
//			if(mYiwanchengText.isSelected()){
//				return;
//			}
//			mDaiquedingText.setSelected(false);
//			mJinxingzhongText.setSelected(false);
//			mYiwanchengText.setSelected(true);
//
//			changeStatus(STATUS_FINISH);
//			break;

		default:
			break;
		}
	}

	
	private void getOrderList(boolean isLoading) {
		if(isLoading){
			startLoadingDialog();
		}
		RequestService.getInstance().getCargoOrderShipperList(this, size+"", cursor+"", action, searchstatus+"", OrderListEntity.class, new RequestListener() {
			
			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if(resultData.isOk()){
					OrderListEntity entity = (OrderListEntity) resultData;
					List<Order> list = entity.getData().getList();
					mDataList.addAll(list);
					
					if ((mDataList.size() < size) || (action.equals(ListViewAction.HISTORY) && list.size() < size)) {
//						mOrderListview.setPullLoadEnable(false);
						mOrderListview.endLoadingMore("没有更多了");
					}
					setAdapter(mDataList);
					
				}else{
					showToast(resultData.getMsg());
				}
				onLoad();
			}
			
			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(msg);
				onLoad();
			}
		});
	}

	protected void setAdapter(List<Order> mDataList2) {
		if(mAdapter == null){
			mAdapter = new OrderListAdapter<Order>(this, mDataList2, false);
			mAdapter.setType(2);
			mOrderListview.setAdapter(mAdapter);
		}else{
			mAdapter.setData(mDataList2);
		}
	}

	private void setListView() {
		mOrderListview.setPullLoadEnable(true);
		mOrderListview.setPullRefreshEnable(true);
		mOrderListview.setXListViewListener(this);
		
		mOrderListview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Order order = (Order) parent.getItemAtPosition(position);
				Intent intent = new Intent(MyOrderTailAfterActivity.this,OrderDetailShipperActivity.class);
				intent.putExtra(ExtraName.orderid, order.getOrderid());
				startActivity(intent);
			}
		});
	}
	
	private void changeStatus(int status) {
		mDataList.clear();
		searchstatus = status;
		onRefresh();
	}
	
	@Override
	public void onRefresh() {
//		if(mDataList.size()>0){
//			cursor = mDataList.get(0).getCursor();
//		}else {
			cursor = "0";
//		}
			mDataList.clear();
		action = ListViewAction.NEW;
		mOrderListview.setPullLoadEnable(true);
		getOrderList(false);
	}

	@Override
	public void onLoadMore() {
		if(mDataList.size()>0){
			cursor = mDataList.get(mDataList.size()-1).getCursor();
		}else {
			cursor = "";
		}
		action = ListViewAction.HISTORY;
		getOrderList(false);
	}

	private void onLoad() {
		mOrderListview.stopRefresh();
		mOrderListview.stopLoadMore();
//		mOrderListview.setRefreshTime(StringHelper.formatDate(System.currentTimeMillis()+""));
	}
	
}
